<?php
/**
 * Displays the information for one or all machines.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-21
 */
require_once 'login_info.php';
require_once 'database.php';
require_once 'machines.php';
require_once 'schedule.php';

loginInfo::update_login ();

/**
 * Displays all of the machines in a list.
 */
function display_all () {
	$db = new database ();
	print "<ul class='phase_list'>\n";
	$result = $db->select_entry ("Machines");
	while ($row = $result->fetch_assoc ()) {
		$id = $row['id']; $name = $row['name'];
		print "<li><a href='/view_machine.php?mach=$id'>$name</a></li>\n";
	}
	print "</ul>\n";
	$db->close ();
}

/**
 * Displays a table with the phase, deadline, and priority of each phase.
 *
 * @param mid		The machine id of the machine to display.
 */
function display_mach ($mid) {
	$db = new database ();
	$mach = new machine ($mid);
	$mach->init($db);

	$mach_file = sprintf ("%s/machine_%d.php", schedule::sched_dir(), (int)$mid);
	include $mach_file;

	if (isset ($GLOBALS['mach_vars']))
		$mach_vars = $GLOBALS['mach_vars'];

	print "<table border=\"1\">
<tr>
<td colspan='3'>Machine: $mach->name</td>
</tr>\n";
	print "<tr>
<td> Order </td>
<td> Deadline </td>
<td> Priority </td>
</tr>\n";
	if (isset ($mach_vars)) {
		foreach ($mach_vars as $phase_info) {
			print "$phase_info";
		}
	}

	print "</table>\n";
	$db->close ();
}
?>
<html>
	<head>
		<title>Opol - Machine View</title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php'; ?>
		<br>
<?php
	if (isset($_GET['mach'])) display_mach((int)$_GET['mach']);
	else display_all ();
?>
	</body>
</html>